package com.kucai.yifaqapp.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.Game_ListBean;
import com.kucai.yifaqapp.model.network.entity.res.exchangeBena;
import com.kucai.yifaqapp.model.network.entity.res.valletDataBean;
import com.kucai.yifaqapp.util.SYSOUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ChessActivity extends BaseActivitySimple {
    private RecyclerView list001;
    private RecyclerView list002;
    private RecyclerView list003;
    private List<valletDataBean> volletname = new ArrayList<>();
    private List<String> money = new ArrayList<>();
    ActivtyAdapter activtyAdapter;
    ActivtyAdapter1 activtyAdapter02;
    ActivtyAdapter2 activtyAdapter03;
    private EditText typenumber;
    private Button conform;
    private int shangjia = -1;
    private int xiajia = -1;
    private LinearLayout skinbg;
    private RelativeLayout title_left_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chess);
        title_left_tv = findViewById(R.id.back);
        skinbg = findViewById(R.id.skinbg);
        valletDataBean bean = new valletDataBean();
        bean.choosename = "我的钱包";
        bean.ischoose = 0;
        bean.chooseId = 0;
        volletname.add(bean);
        money.add("0.00");
        initView();
        initData();
    }

    private void initData() {
//        get_game_czlist 获取游戏列表
        if (Info.user == null) {
            return;
        }
        HttpJsonUtils.getHttpApiInstance()
                .get_game_czlist(HttpAdress.getuseAdress() + "api/GamesApi/list",Info.user.token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Game_ListBean>(this) {
                    @Override
                    protected void onCustomNext(Game_ListBean resBanner) {
                        try {
                            if (resBanner.getCode() == 0) {
                                for (int i = 0; i < resBanner.getData().size(); i++) {
                                    valletDataBean bean = new valletDataBean();
                                    bean.choosename = resBanner.getData().get(i).getName();
                                    bean.ischoose = 0;
                                    bean.chooseId = resBanner.getData().get(i).getG_id();
                                    volletname.add(bean);
                                    money.add("");
                                }
                                GridLayoutManager gridLayoutManager = new GridLayoutManager(ChessActivity.this, 2);
                                //添加布局管理器
                                list001.setLayoutManager(gridLayoutManager);
                                list001.setHasFixedSize(true);
                                activtyAdapter = new ActivtyAdapter();
                                list001.setAdapter(activtyAdapter);
                                GridLayoutManager gridLayoutManager1 = new GridLayoutManager(ChessActivity.this, 2);
                                list002.setLayoutManager(gridLayoutManager1);
                                list002.setHasFixedSize(true);
                                activtyAdapter02 = new ActivtyAdapter1();
                                list002.setAdapter(activtyAdapter02);
                                GridLayoutManager gridLayoutManager2 = new GridLayoutManager(ChessActivity.this, 2);
                                list003.setLayoutManager(gridLayoutManager2);
                                list003.setHasFixedSize(true);
                                activtyAdapter03 = new ActivtyAdapter2();
                                list003.setAdapter(activtyAdapter03);
                            }
                        } catch (Exception e) {

                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }

    private void initView() {
        list001 = findViewById(R.id.list001);
        list002 = findViewById(R.id.list002);
        list003 = findViewById(R.id.list003);
        typenumber = findViewById(R.id.typenumber);
        conform = findViewById(R.id.conform);
        conform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (shangjia == -1) {
                    Toast.makeText(ChessActivity.this, "请选择转出的钱包", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (xiajia == -1) {
                    Toast.makeText(ChessActivity.this, "请选择转入的钱包", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (shangjia == xiajia) {
                    Toast.makeText(ChessActivity.this, "转出不能与转入一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (typenumber.getText().toString().isEmpty()) {
                    Toast.makeText(ChessActivity.this, "请输入金额", Toast.LENGTH_SHORT).show();
                    return;
                }
                //开始转
                HttpJsonUtils.getHttpApiInstance()
                        .get_game_change(HttpAdress.getuseAdress() + "api/GamesApi/moneyExchange",Info.user.token, shangjia, xiajia, typenumber.getText().toString())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new CustomObserver<exchangeBena>(ChessActivity.this) {
                            @Override
                            protected void onCustomNext(exchangeBena resOods) {
                                Toast.makeText(ChessActivity.this, resOods.getMsg() + "", Toast.LENGTH_LONG).show();
                            }

                            @Override
                            protected void onSubscribeCustom(Disposable disposable) {
                            }

                            @Override
                            protected void onErrorCustom(Throwable e) {
                            }
                        });

            }
        });
        title_left_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected int setContentView() {
        return R.layout.activity_chess;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }


    class ActivtyAdapter extends RecyclerView.Adapter<ActivtyAdapter.MViewHolder>
            implements AdapterView.OnItemClickListener {
        @Override
        public ActivtyAdapter.MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ActivtyAdapter.MViewHolder(LayoutInflater.from(ChessActivity.this)
                    .inflate(R.layout.chesslist001, parent, false));

        }

        @Override
        public void onBindViewHolder(final ChessActivity.ActivtyAdapter.MViewHolder holder, final int position) {
//            if (volletname.get(position).ischoose == 0) {
//                holder.item.setBackgroundColor(getResources().getColor(R.color.title_back));
//            } else {
//                holder.item.setBackgroundColor(getResources().getColor(R.color.yellow));
//            }
//            holder.item.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    for (int i = 0; i < volletname.size(); i++) {
//                        volletname.get(i).ischoose = 0;
//                    }
//                    volletname.get(position).ischoose = 1;
//                    notifyDataSetChanged();
//                }
//            });
            holder.my_name.setText(volletname.get(position).choosename);
            if (position == 0) {
                holder.my_money.setText(Info.user.money);
                holder.pic.setVisibility(View.GONE);
            } else {
                holder.my_money.setText(money.get(position)+0.0);
            }
            holder.pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //开始获取余额
                    //开始转
                    HttpJsonUtils.getHttpApiInstance()
                            .get_qipai_yue(HttpAdress.getuseAdress() + "api/GamesApi/action",Info.user.token, volletname.get(position).chooseId, 1)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new CustomObserver<exchangeBena>(ChessActivity.this) {
                                @Override
                                protected void onCustomNext(exchangeBena resOods) {
                                    Toast.makeText(ChessActivity.this, resOods.getMsg() + "", Toast.LENGTH_LONG).show();
                                }

                                @Override
                                protected void onSubscribeCustom(Disposable disposable) {
                                }

                                @Override
                                protected void onErrorCustom(Throwable e) {
                                }
                            });
                }
            });
        }

        @Override
        public int getItemCount() {
            return volletname.size();
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView my_name;
            TextView my_money;
            RelativeLayout item;
            ImageView pic;

            MViewHolder(View itemView) {
                super(itemView);
                item = (RelativeLayout) itemView.findViewById(R.id.item);
                my_name = (TextView) itemView.findViewById(R.id.my_name);
                my_money = itemView.findViewById(R.id.my_money);
                pic = itemView.findViewById(R.id.pic);
            }
        }
    }

    class ActivtyAdapter1 extends RecyclerView.Adapter<ActivtyAdapter1.MViewHolder>
            implements AdapterView.OnItemClickListener {
        @Override
        public ActivtyAdapter1.MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ActivtyAdapter1.MViewHolder(LayoutInflater.from(ChessActivity.this)
                    .inflate(R.layout.chesslist002, parent, false));

        }

        @Override
        public void onBindViewHolder(final ChessActivity.ActivtyAdapter1.MViewHolder holder, final int position) {
            if (volletname.get(position).ischoose == 0) {
                holder.item.setBackgroundColor(getResources().getColor(R.color.title_back));
            } else {
                holder.item.setBackgroundColor(getResources().getColor(R.color.yellow));
            }
            holder.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < volletname.size(); i++) {
                        volletname.get(i).ischoose = 0;
                    }
                    volletname.get(position).ischoose = 1;
                    shangjia = volletname.get(position).chooseId;
                    notifyDataSetChanged();
                }
            });
            holder.my_name.setText(volletname.get(position).choosename);
        }

        @Override
        public int getItemCount() {
            return volletname.size();
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView my_name;
            TextView my_money;
            RelativeLayout item;

            MViewHolder(View itemView) {
                super(itemView);
                my_name = (TextView) itemView.findViewById(R.id.my_name);
                my_money = itemView.findViewById(R.id.my_money);
                item = (RelativeLayout) itemView.findViewById(R.id.item);
            }
        }
    }

    class ActivtyAdapter2 extends RecyclerView.Adapter<ActivtyAdapter2.MViewHolder>
            implements AdapterView.OnItemClickListener {
        @Override
        public ActivtyAdapter2.MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ActivtyAdapter2.MViewHolder(LayoutInflater.from(ChessActivity.this)
                    .inflate(R.layout.chesslist002, parent, false));

        }

        @Override
        public void onBindViewHolder(final ChessActivity.ActivtyAdapter2.MViewHolder holder, final int position) {
            if (volletname.get(position).ischoose == 0) {
                holder.item.setBackgroundColor(getResources().getColor(R.color.title_back));
            } else {
                holder.item.setBackgroundColor(getResources().getColor(R.color.yellow));
            }
            holder.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < volletname.size(); i++) {
                        volletname.get(i).ischoose = 0;
                    }
                    volletname.get(position).ischoose = 1;
                    xiajia = volletname.get(position).chooseId;
                    notifyDataSetChanged();
                }
            });
            holder.my_name.setText(volletname.get(position).choosename);
        }

        @Override
        public int getItemCount() {
            return volletname.size();
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView my_name;
            TextView my_money;
            RelativeLayout item;

            MViewHolder(View itemView) {
                super(itemView);
                my_name = (TextView) itemView.findViewById(R.id.my_name);
                my_money = itemView.findViewById(R.id.my_money);
                item = (RelativeLayout) itemView.findViewById(R.id.item);
            }
        }
    }
}